package com.qk.management.controller;

import com.qk.common.Result;
import com.qk.dto.business.BusinessDTO;
import com.qk.dto.business.BusinessListDTO;
import com.qk.entity.Business;
import com.qk.management.service.BusinessService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 商机管理Controller
 */
@Slf4j
@RestController
@RequestMapping("/businesses")
@SuppressWarnings("all")
public class BusinessController {

    @Autowired
    private BusinessService businessService;

    /**
     * 商机列表查询
     * @param dto 查询条件
     * @return 分页结果
     */
    @GetMapping
    public Result getBusinessList(BusinessListDTO dto) {
        log.info("商机列表查询，参数: {}", dto);
        return Result.success(businessService.getBusinessList(dto));
    }

    /**
     * 公海列表查询
     * @param dto 查询条件
     * @return 分页结果
     */
    @GetMapping("/pool")
    public Result getBusinessPool(BusinessListDTO dto) {
        log.info("公海列表查询，参数: {}", dto);
        return Result.success(businessService.getBusinessPool(dto));
    }

    /**
     * 添加商机
     * @param businessDTO 商机信息
     * @return 操作结果
     */
    @PostMapping
    public Result addBusiness(@RequestBody BusinessDTO businessDTO) {
        log.info("添加商机: {}", businessDTO);
        businessService.addBusiness(businessDTO);
        return Result.success();
    }
    
    /**
     * 分配商机
     * @param businessId 商机ID
     * @param userId 用户ID
     * @return 操作结果
     */
    @PutMapping("/assign/{businessId}/{userId}")
    public Result assignBusiness(@PathVariable Integer businessId, @PathVariable Integer userId) {
        log.info("分配商机，商机ID: {}, 用户ID: {}", businessId, userId);
        businessService.assignBusiness(businessId, userId);
        return Result.success();
    }
    
    /**
     * 退回公海
     * @param id 商机ID
     * @return 操作结果
     */
    @PutMapping("/back/{id}")
    public Result backToPool(@PathVariable Integer id) {
        log.info("退回商机到公海，商机ID: {}", id);
        businessService.backToPool(id);
        return Result.success();
    }
    
    /**
     * 将商机转为客户
     * @param id 商机ID
     * @return 操作结果
     */
    @PostMapping("/toCustomer/{id}")
    public Result toCustomer(@PathVariable Integer id) {
        log.info("将商机转为客户，商机ID: {}", id);
        businessService.toCustomer(id);
        return Result.success();
    }
    
    /**
     * 根据ID查询商机详情
     * @param id 商机ID
     * @return 商机信息
     */
    @GetMapping("/{id}")
    public Result getBusinessById(@PathVariable Integer id) {
        log.info("根据ID查询商机详情，商机ID: {}", id);
        return Result.success(businessService.getBusinessById(id));
    }
    

}